<template>
	<view class="page">
		<u-navbar title="我的心愿" border @leftClick="$utils.back()" placeholder />
		<view class="body">
			<z-paging ref="paging" style="height: 100%;" :fixed="false" v-model="data" @query="query"
				:default-page-no="1" :default-page-size="10">
				<template v-if="!loading">
					<view v-for="item in data" :key="item.id" class="contentItem" @click="jump(item.id)">
						<image mode="aspectFill" :src="item.thumb" />
						<view class="contentInfo">
							<view style="display: flex;align-items: center;justify-content: space-between">
								<view>
									{{item.title}}
								</view>
								<view @click.stop="()=>{}">
									<u-icon name="more-dot-fill" size="50rpx" @click='openSelect(item)'></u-icon>
								</view>
							</view>
							<view style="display: flex;align-items: center;justify-content: space-between">
								<view>
									开始时间:{{item.start_at}}
								</view>
							</view>
						</view>

					</view>
				</template>
				<template v-else>
					<view v-for="item in 8" :key="item" class="contentItem">
						<view class="Skeleton" style="width: 160rpx;height: 160rpx;" />
						<view class="contentInfo">
							<view style="height: 30rpx;width: 100%;" class="Skeleton" v-for="item in 3" :key="item" />
						</view>
					</view>
				</template>
			</z-paging>
		</view>
		<view class="add" @click="jumpAdd" v-if="!loading">
			新增心愿
		</view>
		<root-portal>

			<u-action-sheet :actions="columns" :show="selectShow" @close='selectShow=false' @select="confirm"
				closeOnClickOverlay></u-action-sheet>
			<u-modal :show="delShow" showCancelButton @confirm='del()' @cancel='delShow=false'>
				<view style="padding: 12px 25px 25px 25px;font-size: 30rpx;">
					是否确认删除
				</view>
			</u-modal>
		</root-portal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				data: [],
				loading: true,
				selectItem: {},
				selectShow: false,
				delShow: false,
				disabled: false
			}
		},
		computed: {
			columns() {
				if (this.selectItem.status == 1) {
					return [{
							name: '编辑'
						},
						{
							name: '删除'
						},
					]
				}
				return [{
						name: '编辑'
					}, {
						name: '动态'
					},
					{
						name: '删除'
					},
				]
			}
		},
		methods: {
			jumpActive(id) {
				this.$utils.navigateTo({
					url: '/wish/active/add?id=' + id
				})
			},
			del() {
				this.delShow = false
				if (this.disabled) return
				this.disabled = true
				uni.showLoading({
					title: '删除中...'
				})
				this.$api.Wish.Del(this.selectItem.id).then(err => {
					this.data = this.data.filter(item => item.id != this.selectItem.id)
					this.disabled = false
					uni.hideLoading()
				}).catch(err => {
					uni.hideLoading()
					uni.$u.toast(err.data.message)
					this.disabled = false
				})
			},
			confirm(e) {
				this.selectShow = false
				if (e.name == '编辑') {
					this.$utils.navigateTo({
						url: '/wish/update?id=' + this.selectItem.id
					})
				} else if (e.name == '删除') {
					this.delShow = true
				} else if (e.name == '动态') {
					this.jumpActive(this.selectItem.id)
				}
			},
			openSelect(item) {
				this.selectShow = true
				this.selectItem = item
			},
			jumpAdd() {
				this.$utils.navigateTo({
					url: '/wish/add'
				})
			},
			jump(id) {
				this.$utils.navigateTo({
					url: '/wish/detail?id=' + id
				})
			},
			query(page, size) {
				let obj = {
					page,
					size
				}
				uni.showLoading({
					title: '加载中...'
				})
				this.$api.My.Wish(obj).then(res => {
					uni.hideLoading()
					this.$refs.paging.complete(res.data.wishes.data)
					this.loading = false
				}).catch(err => {
					uni.hideLoading()
					uni.$u.toast(err.data.message)
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.add {
		border-radius: 100rpx;
		background-color: #ddd;
		text-align: center;
		padding: 20rpx;
		box-sizing: border-box;
	}

	.page {
		padding: 0rpx 20rpx;
		height: 100vh;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 10rpx);
		/* 兼容 iOS 设备 */
		padding-bottom: calc(env(safe-area-inset-bottom) + 10rpx);
		box-sizing: border-box;
		display: flex;
		flex-direction: column;
	}

	.body {
		flex: 1;
		overflow: hidden;
	}

	.contentItem {
		padding: 20rpx 0;
		display: flex;
		border-bottom: 1rpx solid #f5f5f5;

		image {
			width: 160rpx;
			height: 160rpx;
			border-radius: 8rpx;
		}

		.contentInfo {
			flex: 1;
			margin: 0 20rpx;
			display: flex;
			flex-direction: column;
			height: 160rpx;
			justify-content: space-between;
		}

		.icon {
			background-color: #ddd;
			border-radius: 100rpx;
			padding: 8rpx 20rpx;
			display: inline-block;
			margin-right: 10rpx;
		}
	}
</style>